﻿using Entity.Front_Desk;
using Entity.ResultModel;
using IHotelBLL.IEmployeeBLL;
using IHotelDAL.IFrondeskDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelBLL.FrontdeskBLL
{
    public class RoleBLL:IRoleBLL
    {
        IRoleDAL _roleDAL;
        IFrontDeskDAL _frontDeskDAL;
        IEmployeeRoleDAL _employeeRoleDAL;
        IRole_MenuDAL _roleMenuDAL;
        IMenuDAL _menuDAL;
        public RoleBLL(IRoleDAL roleDAL, IFrontDeskDAL frontDeskDAL, IEmployeeRoleDAL employeeRoleDAL, IRole_MenuDAL roleMenuDAL,IMenuDAL menuDAL)
        {
            _roleDAL = roleDAL;
            _frontDeskDAL = frontDeskDAL;
            _employeeRoleDAL = employeeRoleDAL;
            _roleMenuDAL = roleMenuDAL;
            _menuDAL = menuDAL;
        }
        public bool AddRole(string roleName,out string msg)
        {
            Role roomType = _roleDAL.GetEntities().FirstOrDefault(c => c.RoleName == roleName);
            if (roomType != null)
            {
                msg = "已存在";
                return false;
            }
            Role floor = new Role()
            {
                Id = Guid.NewGuid().ToString(),
                RoleName = roleName,
            };
            bool isSuccess = _roleDAL.CreateEntity(floor);
            msg = isSuccess ? "添加成功" : "添加失败";
            return isSuccess;
        }


    
        public bool UpdateRole(string id,string roleName,out string msg)
        {
            Role role = GetRole(id);
            if (role == null)
            {
                msg = "未找到客房ID";
                return false;
            }
            role.RoleName = roleName;


            bool isSuccess =_roleDAL.UpdateEntity(role);
            if (isSuccess)
            {
                msg = "修改成功";
                return true;
            }
            else
            {
                msg = "修改失败";
                return true;
            }
        }
        public bool DeleteRole(string id,out string msg)
        {
            bool isSuccess = _roleDAL.DeleteEntity(id);
            msg = isSuccess ? "删除成功" : "删除失败";
            return isSuccess;
        }
        public List<Role> GetRoles(int page,int limit,out int count)
        {
            var datas =_roleDAL.GetEntities();
            count = datas.Count();
            var roleInfos = datas.Skip((page - 1) * limit).Take(limit).Select(r => new
            {
                r.Id,
                r.RoleName
            }).ToList();
            return roleInfos.Select(r => new Role()
            {
                Id = r.Id,
                RoleName = r.RoleName,
            }).ToList();
        }
        public List<Role> GetRoles()
        {
            return _roleDAL.GetEntities().ToList();
        }

        public Role GetRole(string id)
        {
            return _roleDAL.GetEntities().Where(r => r.Id == id).FirstOrDefault();
        }

        public void BindRole(List<string> employeeIds,string roleId)
        {
            //先查询数据库中当前角色的所有绑定数据
            List<EmployeeRole> employeeRoles = _employeeRoleDAL.GetEntities().Where(r => r.RoleId == roleId).ToList();

            foreach (var item in employeeRoles)
            {
                //使用当前要绑定的用户id集合去判断，如果包含当前遍历的这条用户id，则不用删除。否则就是需要删除的数据
                if (!employeeIds.Contains(item.EmployeeId))
                {
                    _employeeRoleDAL.DeleteEntity(item);
                }
            }

            //根据角色获取当时已经绑定的用户id集合
            //List<string> bindUserIds = r_UserInfo_RoleInfos.Select(r => r.UserId).ToList();

            foreach (var userId in employeeIds)
            {
                //any：遍历集合中的元素，当有一条满足条件就返回true，反之返回false
                if (!employeeRoles.Any(r => r.EmployeeId == userId))
                {
                    EmployeeRole entity = new EmployeeRole()
                    {
                        Id = Guid.NewGuid().ToString(),
                        RoleId = roleId,
                        EmployeeId = userId
                    };
                    _employeeRoleDAL.CreateEntity(entity);
                }
            }
        }

        public List<string> GetEmployee(string id)
        {
            return _employeeRoleDAL.GetEntities().Where(r => r.RoleId == id).Select(r => r.EmployeeId).ToList();
        }
        public List<TransferOptionModel> GetTransferOptions()
        {
            return _frontDeskDAL.GetEntities().Select(u => new TransferOptionModel
            {
                Value = u.Id,
                Title = u.EmployeeName
            }).ToList();
        }

        public void BindEmployee(List<string> employeeIds, string roleId)
        {
            List<EmployeeRole> employeeRoles = _employeeRoleDAL.GetEntities().Where(r => r.RoleId == roleId).ToList();

            foreach (var item in employeeRoles)
            {
                //使用当前要绑定的用户id集合去判断，如果包含当前遍历的这条用户id，则不用删除。否则就是需要删除的数据
                if (!employeeIds.Contains(item.EmployeeId))
                {
                    _employeeRoleDAL.DeleteEntity(item);
                }
            }

            foreach (var userId in employeeIds)
            {
                //any：遍历集合中的元素，当有一条满足条件就返回true，反之返回false
                if (!employeeRoles.Any(r => r.EmployeeId == userId))
                {
                    EmployeeRole entity = new EmployeeRole()
                    {
                        Id = Guid.NewGuid().ToString(),
                        RoleId = roleId,
                        EmployeeId = userId
                    };
                    _employeeRoleDAL.CreateEntity(entity);
                }
            }
        }

        public List<TransferOptionModel> GetMenuTransferOptions()
        {
            return _menuDAL.GetEntities().Select(u => new TransferOptionModel
            {
                Value = u.Id,
                Title = u.Title
            }).ToList();
        }

        public List<string> GetBindMenuIds(string roleId)
        {
            return _roleMenuDAL.GetEntities().Where(r => r.RoleId == roleId).Select(r => r.MenuId).ToList();
        }

        public void BindMenu(List<string> menuIds, string roleId)
        {
            //先查询数据库中当前角色的所有绑定数据
            List<Role_Menu> role_Menus = _roleMenuDAL.GetEntities().Where(r => r.RoleId == roleId).ToList();

            foreach (var item in role_Menus)
            {
                //使用当前要绑定的用户id集合去判断，如果包含当前遍历的这条用户id，则不用删除。否则就是需要删除的数据
                if (!menuIds.Contains(item.MenuId))
                {
                    _roleMenuDAL.DeleteEntity(item);
                }
            }

            foreach (var menuid in menuIds)
            {
                //any：遍历集合中的元素，当有一条满足条件就返回true，反之返回false
                if (!role_Menus.Any(r => r.MenuId == menuid))
                {
                    Role_Menu entity = new Role_Menu()
                    {
                        Id = Guid.NewGuid().ToString(),
                        RoleId = roleId,
                        MenuId = menuid
                    };
                    _roleMenuDAL.CreateEntity(entity);
                }
            }
        }
    }
}